﻿<%@ WebHandler Language="C#" Class="ProtectedPhoneConfigMgr" %>

using System;
using System.Web;
using System.Data;
using System.Text;
using Chenxin.Lib.Utility;
public class ProtectedPhoneConfigMgr : AjaxHandlerBase
{
    public override void RequestProcess(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        
        switch (context.Request["op"].ToLower())
        {
            case "getlist":
                GetList(context);
                break;
            case "add":
                Add(context);
                break;
            case "del":
                Del(context);
                break;
            case "edit":
                Edit(context);
                break;
            case "getmodel":
                GetModel(context);
                break;
            case "getppclist"://获取电话保护配置列表
                GetPPCList(context);
                break;
        }
    }



    /// <summary>
    /// 获取电话保护配置列表
    /// </summary>
    /// <param name="context"></param>
    private void GetPPCList(HttpContext context)
    {
        try
        {
            StringBuilder strWhere = new StringBuilder();
            strWhere.AppendFormat(" CityId='{0}'", CurrentUserInfo.CityId);
            strWhere.AppendFormat(" and CompanyId='{0}'", CurrentUserInfo.CompanyId);
            DataSet ds = Chenxin.Lib.House.DAL.ProtectedPhoneConfig.GetList(strWhere.ToString());

            context.Response.Write("{sucess:1,msg:'',data:{" + JsonHelp.Convert2Json(ds).Replace("rows", "ppc") + "}}");
        }
        catch (Exception e)
        {
            context.Response.Write("{success:0,msg:'获取配置失败！" + e.Message + "',data:''}");
        }
    }
    

    /// <summary>
    /// 获取Model
    /// </summary>
    /// <param name="context"></param>
    private void GetModel(HttpContext context)
    {
        try
        {
            Chenxin.Lib.House.Entity.ProtectedPhoneConfig model = null;

            model = Chenxin.Lib.House.DAL.ProtectedPhoneConfig.GetModel(new Guid(context.Request["modelid"]));

            if (model != null)
            {
                context.Response.Write(("{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}"));
            }
            else
            {
                context.Response.Write("{success:'0',msg:'获取记录信息失败！',data:''}");
            }
        }
        catch(Exception e)
        {
            context.Response.Write(("{success:'0',msg:'获取记录信息失败！" + e.Message + "',data:{model:{}}}"));
        }
    }

    /// <summary>
    /// 添加一条记录
    /// </summary>
    /// <param name="context"></param>
    private void Add(HttpContext context)
    {
        try        
        {
            if (context.Request["model"] != null)
            {
                Chenxin.Lib.House.Entity.ProtectedPhoneConfig model = (Chenxin.Lib.House.Entity.ProtectedPhoneConfig)JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.House.Entity.ProtectedPhoneConfig));
                model.ConfigId = Guid.NewGuid();
                Chenxin.Lib.House.DAL.ProtectedPhoneConfig.Add(model);
                context.Response.Write(string.Format("{{success:'1',msg:'添加成功！',data:{{ConfigId:'{0}'}}}}", model.ConfigId));
            }
            else
            {
                context.Response.Write("{success:'0',msg:'添加失败！',data:''}");
            }
            
        }
        catch(Exception wrong)
        {
            context.Response.Write("{success:'0',msg:'添加失败！原因：" + wrong.Message.Replace("'","\'") + "',data:''}");          
        }
    }

    private void Edit(HttpContext context)
    {
        try
        {
            if (context.Request["model"] != null)
            {
                Chenxin.Lib.House.Entity.ProtectedPhoneConfig model = (Chenxin.Lib.House.Entity.ProtectedPhoneConfig)JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.House.Entity.ProtectedPhoneConfig));
                Chenxin.Lib.House.DAL.ProtectedPhoneConfig.Update(model);
                context.Response.Write(string.Format("{{success:'1',msg:'编辑成功！',data:{{ConfigId:'{0}'}}}}", model.ConfigId));
            }
            else
            {
                context.Response.Write("{success:'0',msg:'编辑失败！',data:''}");
            }

        }
        catch (Exception wrong)
        {
            context.Response.Write("{success:'0',msg:'编辑失败！原因：" + wrong.Message.Replace("'", "\'") + "',data:''}");
        }        
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="context"></param>
    private void Del(HttpContext context)
    {
        try
        {
            if (context.Request["modelid"] != null)
            {
                string idList = context.Request["modelid"];
                idList = idList.Replace(",", "','");

                Chenxin.Lib.House.DAL.ProtectedPhoneConfig.Delete(string.Format(" ConfigId in ('{0}')", idList));
                context.Response.Write("{success:'1',msg:'删除成功！',data:''}");
            }
            else
            {
                context.Response.Write("{success:'0',msg:'请至少选择一条记录！',data:''}");
            }
        }
        catch (Exception e)
        {
            context.Response.Write("{success:'0',msg:'删除失败！原因：" + e.Message.Replace("'", "\'") + "',data:''}");
        }
    }
    
    /// <summary>
    /// 获取数据列表
    /// </summary>
    /// <param name="context"></param>
    private void GetList(HttpContext context)
    {
        try
        {
            int pageSize = 20;
            int pageIndex = 1;
            string order = " ConfigType desc";
            int count = 0;
            if (context.Request["pagesize"] != null)
            {
                pageSize = Convert.ToInt32(context.Request["pagesize"]);
            }
            if (context.Request["pageindex"] != null)
            {
                pageIndex = Convert.ToInt32(context.Request["pageindex"]) + 1;
            }

            StringBuilder strWhere = new StringBuilder("1=1");
            if (context.Request["cityid"] != null)
            {
                strWhere.AppendFormat(" and CityId='{0}'", context.Request["cityid"]);
            }
            if (context.Request["companyid"] != null)
            {
                strWhere.AppendFormat(" and CompanyId='{0}'", context.Request["companyid"]);
            }
            if (context.Request["configtype"] != null)
            {
                strWhere.AppendFormat(" and ConfigType like '%{0}%'", context.Request["configtype"]);
            }
            if (context.Request["sort"] != null&&context.Request["sort"].Trim()!="")
            {
                order = context.Request["sort"];
            }

            DataSet ds = Chenxin.Lib.House.DAL.ProtectedPhoneConfig.GetList(pageIndex, pageSize, order, strWhere.ToString(), "*", true);
            count = Convert.ToInt32(ds.Tables[1].Rows[0].ItemArray[0]);


            context.Response.Write(string.Format("{{success:'1',data:{{html:'{0}',count:{1}}}}}", CreateDlGrid(ds.Tables[0], "ConfigType,ConfigMinValue,ConfigMaxValue,ConfigValue,ConfigControlType,City,Company", "ConfigId"), count));
        }
        catch (Exception e)
        {
            context.Response.Write(string.Format("{{success:'0',data:{{html:'{0}',count:{1}}}}}", "", 0));
        
        }
    }
    

}